package com.mk.admin.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.mk.admin.mapper.MedicalTechnologyProjectMapper;
import com.mk.admin.mapper.SelfBillingProjectMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.SelfBillingDto;
import com.mk.admin.model.dto.SelfBillingProjectDto;
import com.mk.admin.model.dto.SelfBillingProjectRemoteDto;
import com.mk.admin.model.entity.MedicalTechnologyProjectEntity;
import com.mk.admin.model.entity.SelfBillingEntity;
import com.mk.admin.model.entity.SelfBillingProjectEntity;
import com.mk.admin.model.query.MedicalTechnologyProjectQuery;
import com.mk.admin.model.query.SelfBillingProjectQuery;
import com.mk.admin.model.query.SelfBillingProjectRemoteQuery;
import com.mk.admin.model.vo.MedicalTechnologyProjectRemoteVo;
import com.mk.admin.model.vo.MedicalTechnologyProjectVo;
import com.mk.admin.model.vo.SelfBillingProjectVo;
import com.mk.admin.service.MedicalTechnologyProjectService;
import com.mk.admin.service.SelfBillingProjectService;
import com.mk.admin.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SelfBillingProjectServiceImpl implements SelfBillingProjectService {
    @Autowired
    private SelfBillingProjectMapper selfBillingProjectMapper;

    @Override
    public ResultDto select(SelfBillingProjectQuery query) {
        SelfBillingProjectEntity entity = JSONCopy.copy(query, SelfBillingProjectEntity.class);
        return ResultDto.data(selfBillingProjectMapper.count(entity), selfBillingProjectMapper.select(entity));
    }

    @Override
    public ResultDto add(SelfBillingProjectVo vo) {
        SelfBillingProjectEntity entity = JSONCopy.copy(vo, SelfBillingProjectEntity.class);
        return ResultDto.data(selfBillingProjectMapper.insert(entity));
    }

    @Override
    public ResultDto update(SelfBillingProjectVo vo) {
        SelfBillingProjectEntity entity = JSONCopy.copy(vo, SelfBillingProjectEntity.class);
        return ResultDto.data(selfBillingProjectMapper.updateById(entity));
    }

    @Override
    public ResultDto delete(int id) {
        return ResultDto.data(selfBillingProjectMapper.deleteById(id));
    }

    @Override
    public ResultDto status(int id) {
        return ResultDto.data(selfBillingProjectMapper.status(id));
    }


    @Override
    public ResultDto fresh(SelfBillingProjectRemoteQuery query) {
        String result = "";
        if (query.getMedicaType() != null && !"".equals(query.getMedicaType())) {
            result = insert(query);
        } else {
            query = new SelfBillingProjectRemoteQuery();

            query.setMedicaType("05");
            result = insert(query);

            query.setMedicaType("06");
            result += insert(query);
        }
        return ResultDto.data("success", "获得 " + result);
    }

    private String insert(SelfBillingProjectRemoteQuery query) {
        JSONObject jsonObject = HttpTool.postJson("com_0009", query);
        Object result = jsonObject.get("result");
        List<SelfBillingProjectRemoteDto> list = new ArrayList<>();
        if (result != null && !"[]".equals(result)) {
            list = JSONArray.parseArray(result.toString(), SelfBillingProjectRemoteDto.class);
            for (SelfBillingProjectRemoteDto item : list
            ) {
                SelfBillingProjectEntity entity = new SelfBillingProjectEntity();
                entity.setCode(item.getSetMealCode());
                List<SelfBillingProjectDto> select = selfBillingProjectMapper.select(entity);
                entity.setMoney(NumberTool.strToMoney(item.getSetMealMoney()));
                entity.setName(item.getSetMealName());
                entity.setMedicalType(query.getMedicaType());
                entity.setStatus(1);
                entity.setDoctorCode(item.getBillDoctorCode());
                entity.setDoctorName(item.getBillDoctorName());
                entity.setDepartmentCode(item.getExecDeptCode());
                entity.setDepartmentName(item.getExecDeptName());
<<<<<<< HEAD
=======
                entity.setIsSelfbilling(0);
                entity.setCanReserve(0);
>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac
                if ("05".equals(query.getMedicaType())) {
                    entity.setMedicalTypeName("检验");
                }
                if ("06".equals(query.getMedicaType())) {
                    entity.setMedicalTypeName("检查");
                }
                if (!StringTool.isBlank(item.getSetMealDes())) {
                    entity.setDescribe(item.getSetMealDes());
                }
                if (select.size() == 0) {
                    entity.setCreateTime(DateTool.format());
                    selfBillingProjectMapper.insert(entity);
                } else {
                    entity.setId(select.get(0).getId());
                    selfBillingProjectMapper.updateById(entity);
                }
            }
        }
<<<<<<< HEAD
        return list.size() + "条 " + ("05".equals(query.getMedicaType()) ? "检验 " : "检查 ");
=======
        return list.size() + " 条 " + ("05".equals(query.getMedicaType()) ? "检验 " : "检查 ");
>>>>>>> f2106939b942a7ac2c3470910120a35364fbfaac
    }

}
